﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using IMDBApi.Implementation.Models;

namespace IMDBApi.Implementation
{
    public static class PollingUtils
    {
        /// <summary>
        /// ...
        /// </summary>
        private static readonly List<string> extList = new List<string>() { ".AVI", ".MPEG" };

        /// <summary>
        /// Gets a list of unrated files.
        /// </summary>
        /// <returns>Returns unrated files.</returns>
        public static List<FileInfo> GetUnratedFiles(string path)
        {
            var allFiles = Directory.GetFiles(path);

            var unratedFiles = from file in allFiles
                                    where Regex.Match(file, @"^(?!.*rating.*).*$", RegexOptions.IgnoreCase | RegexOptions.Multiline).Success
                                        select new FileInfo(file);

            return unratedFiles.ToList();
        }

        /// <summary>
        /// ...
        /// </summary>
        public static List<FileInfo> LoadJSONFiles()
        {
            List<FileInfo> tmp = new List<FileInfo>();

            FileInfo[] files = (from file in new DirectoryInfo(Configurations.MOVIE_LIB).GetFiles()
                                    where ".JSON".Contains(file.Extension.ToUpper())
                                        select file).ToArray();

            foreach (FileInfo file in files)
            {
                tmp.Add(file);
            }

            return tmp;
        }

        /// <summary>
        /// ...
        /// </summary>
        public static List<FileInfo> LoadMovieFiles()
        {
            List<FileInfo> tmp = new List<FileInfo>();

            FileInfo[] files = (from file in new DirectoryInfo(Configurations.MOVIE_LIB).GetFiles()
                                    where extList.Contains(file.Extension.ToUpper())
                                        select file).ToArray();

            foreach (FileInfo file in files)
            {
                tmp.Add(file);
            }

            return tmp;
        }

        /// <summary>
        /// Load movies and json files. Returns movie list.
        /// </summary>
        /// <returns>Returns list of movies</returns>
        public static List<MovieFile> LoadFiles()
        {
            List<MovieFile> tmp = new List<MovieFile>();

            List<FileInfo> movieFiles = PollingUtils.LoadMovieFiles();
            List<FileInfo> jsonFiles = PollingUtils.LoadJSONFiles();

            List<FileInfo> localJsonFiles = (from m in movieFiles
                                                join j in jsonFiles on Path.GetFileNameWithoutExtension(m.Name) equals Path.GetFileNameWithoutExtension(j.Name)
                                                    select m).ToList();

            // For all files having json data locally
            foreach (FileInfo file in localJsonFiles)
            {
                tmp.Add(new MovieFile(file.FullName, Path.GetFileNameWithoutExtension(file.Name), file.Extension, file.Directory.FullName, DownloadState.Local));
            }

            List<FileInfo> missingJsonFiles = movieFiles.Except<FileInfo>(localJsonFiles).ToList();

            // For all files not having json data locally
            foreach (FileInfo file in missingJsonFiles)
            {
                tmp.Add(new MovieFile(file.FullName, Path.GetFileNameWithoutExtension(file.Name), file.Extension, file.Directory.FullName, DownloadState.Missing));
            }

            return tmp;
        }
    }
}
